<?php
namespace app\api\controller\wanlshop;
use app\common\controller\Api;
use fast\Random;
use think\Db;
use think\Config;
class Orderitem extends Api
{
	//protected $noNeedLogin = ['*'];
	protected $noNeedRight = ['*'];
    
    public $type_array = [
                    //1=有限次卡,2=不限次卡,3=通卡,4=充值卡,5=折扣卡
                    1 => '有限次卡',
                    2 => '不限次卡',
                    3 => '通卡',
                    4 => '充值卡',
                    5 => '折扣卡'
            ];
	/**
	 * 获取卡项列表
	 */
	public function getlist($state = null)
	{
		$con = [];
		$con['user_id'] = $this->auth->id;
		$con['status'] = 'normal';
		$list = model('app\admin\model\wanlshop\OrderItem')
		        ->where($con)
		        ->order('createtime desc')
		        ->paginate();
		$list = $list->toArray();
		//dump($list);
		if(!empty($list['data']))
		{
			foreach($list['data'] as &$value)
			{
				//处理剩余如果是次卡就去除小数点
				if($value['type'] == 1 || $value['type'] == 2)
				{
					$value['spent'] = floor($value['spent']);
				}
				$value['type_text'] = $this->type_array[$value['type']];
				$value['is_available'] = 1;
				$value['item_type_unit']=config('item_type_unit')[$value['type']]??'';
				$_shop_name = model('app\admin\model\store\Shop')->where('id',$value['shop_id'])->value('shopname');
				$value['shop_name'] = empty($_shop_name)?'无':$_shop_name;
				if($value['validity'] == 9999){
					$value['validitytime'] = '永久有效';
				}
				if ($value['validity'] == 1 && $value['validitytime'] < time()) {
					$value['is_available'] = 0;
				}
				if ($value['status'] == 'hidden') {
					$value['is_available'] = 0;
				}
				$value['bgcolor'] = '#e4c477';
				if($value['type'] == 4){
					$value['bgcolor'] = '#626ea4';
				}
			}
		}

		$list ? ($this->success('ok',$list)) : ($this->error(__('网络繁忙')));
	}
}